using System;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.Logic;


namespace IQIGame.Onigao.GamePlay
{
    public class BuffData : ISerializeCustomPOD, IReusableClassWithFree
    {
        /// <summary>
        /// ID
        /// </summary>
        public int buffID;

        /// <summary>
        /// 发起者
        /// </summary>
        public long maker;

        /// <summary>
        /// 持有者
        /// </summary>
        public long target;

        /// <summary>
        /// 持续时间
        /// </summary>
        public int timeDuration;

        /// <summary>
        /// 经过时间
        /// </summary>
        public int timeStart;

        /// <summary>
        /// 层数
        /// </summary>
        public int stack { get; private set; }

        /// <summary>
        /// 最大堆叠层数
        /// </summary>
        public int maxStack;

        /// <summary>
        /// buff组
        /// </summary>
        public int gourp;

        public void Serialize(SOutputStream _buf)
        {
            _buf.Write_int(this.buffID);
            _buf.Write_long(this.maker);
            _buf.Write_long(this.target);
            _buf.Write_int(this.timeDuration);
            _buf.Write_int(this.timeStart);
            _buf.Write_int(this.stack);
            _buf.Write_int(this.maxStack);
            _buf.Write_int(this.gourp);
        }

        public void DeSerialize(SInputStream _buf)
        {
            this.buffID = _buf.Read_int();
            this.maker = _buf.Read_long();
            this.target = _buf.Read_long();
            this.timeDuration = _buf.Read_int();
            this.timeStart = _buf.Read_int();
            this.stack = _buf.Read_int();
            this.maxStack = _buf.Read_int();
            this.gourp = _buf.Read_int();
        }

        public static BuffData Create(BuffHandler rHandler)
        {
            if (rHandler == null)
            {
                return null;
            }
            var rData = ClassPool.Get<BuffData>();
            rData.buffID = rHandler.buffID;
            rData.maker = rHandler.maker.unit.GUID;
            rData.target = rHandler.target.unit.GUID;
            rData.timeDuration = rHandler.timeDuration;
            rData.timeStart = rHandler.timeStart;
            rData.stack = rHandler.stack;
            rData.maxStack = rHandler.maxStack;
            rData.gourp = rHandler.gourp;
            return rData;
        }

        public uint MaxStore => 15;
        public void OnReset()
        {
            this.buffID = default;
            this.maker = default;
            this.target = default;
            this.gourp = default;
            this.maxStack = default;
            this.timeDuration = default;
            this.timeStart = default;
            this.stack = default;
        }

        public void Free()
        {
            ClassPool.Put(this);
        }
    }
}